<template>
  <div class="online-stats">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      class="search-form"
    >
      <el-form-item label="上线时间:">
        <el-date-picker
          v-model="date"
          size="small"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="在线状态:">
        <el-select
          v-model="form.online"
          size="small"
          placeholder="在线状态">
          <el-option
            v-for="item in driverOnlineStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="司机姓名:">
        <el-input
          v-model="form.driver"
          size="small"
          placeholder="请输入司机姓名"
        ></el-input>

      </el-form-item>
      <el-form-item label="司机账号:">
        <el-input
          v-model="form.account"
          size="small"
          placeholder="请输入司机账号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="search(1)"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <!--    <div class="operate-bar">-->
    <!--      <el-button-->
    <!--        size="small"-->
    <!--        @click="exportExcel"-->
    <!--      >导出excel-->
    <!--      </el-button>-->
    <!--    </div>-->
    <el-table
      :data="tableData"
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column
        prop="account"
        label="司机账号"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="司机姓名"
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号码"
        width="120px"
      >
      </el-table-column>
      <el-table-column
        prop="count"
        label="任务数"
        width="100px"
      >
      </el-table-column>
      <el-table-column
        prop="money"
        label="收入总计"
        width="100px"
      >
      </el-table-column>
      <el-table-column
        prop="online_time_str"
        label="在线时长"
        width="150px"
      >
      </el-table-column>
      <el-table-column
        prop="online_text"
        label="在线状态"
        width="80px"
      >
      </el-table-column>
      <el-table-column
        prop="last_online_time"
        label="上线时间"
        width="180px"
      >
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size.sync="form.size"
        :current-page.sync="form.page"
        :total="total"
        :hide-on-single-page="true"
        @current-change="search"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-14
   * @Project: driver
   */
  import {pageSize, driverOnlineStatus, driverActivateStatus} from "../../constant"

  export default {
    name: "online-stats"
    , data() {
      return {
        driverOnlineStatus
        , form: {
          page: 1
          , size: pageSize
          , time_begin: ''
          , time_end: ''
          , online: 3
          , driver: ''
          , account: ''
        }
        , total: 0
        , loading: false
        , date: []
        , tableData: []
      }
    }
    , watch: {
      date(date) {
        let start = ''
          , end = ''
        if (date) {
          [start, end] = date
        }
        this.form.time_begin = start
        this.form.time_end = end
      }
    }
    , mounted() {
      this.search(1)
    }
    , methods: {
      search(page) {
        this.form.page = page
        this.getList()
      }
      , getList() {
        this.loading = true
        this.$axios.get('/api/v1/driver/online/records', {
          params: this.form
        })
          .then(res => {
            let {total, data} = res
            this.total = total || 0
            let list = data || []
            this.tableData = list.map(item => {
              for (let i = 0; i < driverActivateStatus.length; i++) {
                if (driverActivateStatus[i].value === item.online) {
                  item.online_text = driverActivateStatus[i].label
                  break
                }
              }
              item.online_time_str = this.getTimeStr(item.online_time)
              return item
            })
          })
          .catch(() => {

          })
          .finally(() => {
            this.loading = false
          })
      }
      , getTimeStr(online_time) {
        let ret = ''
          , aHour = 60 * 60
          , aMinute = 60
        if (online_time > aHour) {
          let hour = Math.floor(online_time / aHour)
          ret += hour + '小时'
          online_time = online_time - aHour * hour
        }
        if (online_time > aMinute) {
          let minute = Math.floor(online_time / aMinute)
          {
            ret += minute + '分钟'
          }
          online_time = online_time - aMinute * minute
        }
        if (online_time > 0) {
          ret += online_time + '秒'
        }
        return ret
      }
      , exportExcel() {

      }
    }
  }
</script>